package com.autoai.animation.demo;


import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import com.autoai.animation.demo.customdialog.LoginCustomDialog;
import com.autoai.animation.demo.customdialog.MusicCustomDialog;
import com.autoai.animation.demo.customdialog.QrCustomDialog;

/**
 * @author Wangjw
 * @date 2020-12-17
 * @brief
 */
public class ThirdFragment extends Fragment {

    private ConstraintLayout cl_group;
    private SurfaceView vrVideo;
    private MediaPlayer mediaPlayer;
    Button bt1;
    Button bt2;
    Button bt3;
    Button bt4;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.lay_third_fragment, container, false);
        cl_group = view.findViewById(R.id.cl_group);
        bt1 = view.findViewById(R.id.bt_1);
        bt2 = view.findViewById(R.id.bt_2);
        bt3 = view.findViewById(R.id.bt_3);
        bt4 = view.findViewById(R.id.bt_4);
//        vrVideo = view.findViewById(R.id.vr_video);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initClick();
    }

    private void initClick() {
//        initData();
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WallpaperControl.getInstance().changeAnim();
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicCustomDialog.getInstance(getActivity()).show();
            }
        });
        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QrCustomDialog.getInstance(getActivity()).show();
            }
        });
        bt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginCustomDialog.getInstance(getActivity()).show();
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
//        if (mediaPlayer != null) {
//            mediaPlayer.stop();
//        }
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    private void initData() {
        Uri uri = Uri.parse("android.resource://com.autoai.animation.demo" + "/" + R.raw.vui_bg);
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start();
            }
        });
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mediaPlayer.start();
//                mediaPlayer.stop();
//                vrVideo.setVisibility(View.GONE);
            }
        });
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    mediaPlayer.setDisplay(vrVideo.getHolder());
                    mediaPlayer.setDataSource(getActivity(), uri);
                    mediaPlayer.prepareAsync();
                } catch (Exception e) {   ///在这里增加播放失败.
                    mediaPlayer.release();
                    if (mediaPlayer != null) {
                        e.printStackTrace();
                    }
                }
            }
        }, 200);
    }


}
